package main

import (
	"fmt"
	"math/rand"
	"time"
)

func msgGen() chan string {
	c := make(chan string)
	go func() {
		i := 0
		for {
			time.Sleep(time.Duration(rand.Intn(2000)) * time.Millisecond)
			c <- fmt.Sprintf("message %d", i)
			i++
		}
	}()

	return c
}

func fanIn(c1, c2 chan string) chan string {
	c := make(chan string)
	go func() {
		for {
			c <- <-c1
		}
	}()

	go func() {
		for {
			c <- <-c2
		}
	}()

	return c
}

func main() {
	m1 := msgGen()
	m2 := msgGen()
	m := fanIn(m1, m2)
	for {
		fmt.Println(<-m)
	}

}
